﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CheckBut_Click(object sender, EventArgs e)
    {

        if (AccountTex.Text == "")
        {
            Response.Write(@"<script>alert('用户名不能为空！');</script>");
            return;
        }
        if (checkAccountExit() == true)
        {//存在用户
            AccountLab.Text = "账号已存在";
        }
        else //不存在用户
        {
            AccountLab.Text = "账号可用";
        }
        return;
    }

    protected void RegisterBut_Click(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrCustomer"].ConnectionString;
        string username = AccountTex.Text;
        string password = PswTex.Text;
        string rePas = RePswTex.Text;
        string name = nameTex.Text;
        bool sex ;
        int age ;
        string reference = referenceTex.Text;
        string adr = adrTex.Text;
        string tel = telTex.Text;
        int level = 0;

        if (username == "" || password == "" || rePas == "" || name == "" )
        {
            MesgLab.Text = "<span style=\"color:red\">请认真填写(*)内容</span>";
            return;
        }
        else
            MesgLab.Text = "";
        if(username.Length >20 || name.Length>11 || password.Length >35 || reference.Length >200 || adr.Length > 100 || tel.Length > 11)
        {
            MesgLab.Text = "<span style=\"color:red\">请认真填写(*)内容</span>";
            return;
        }        
        else
            MesgLab.Text = "";

        if (password != rePas)
        {
            MesgLab.Text = "<span style=\"color:red\">重复密码不一致</span>";
            return;
        }
        else
            MesgLab.Text = "";

        if (ageTex.Text == "")
            age = -1;
        else
            age = int.Parse(ageTex.Text);

        if(maleRadio.Checked)
            sex = true;//男生
        else
            sex = false;//女生
        if(checkAccountExit() == true)
        {
            AccountLab.Text = "账号已存在";
            return;
        }

        string sql = "Insert into Customer_Account(Aaccount, Apswd,Aname,Asex,Aage,AvipLel,Aref,Aadr,Atel) values(@username,@password,@name,@sex,@age,@level,@reference,@adr,@tel)";
        SqlParameter[] parameters = { new SqlParameter("@username", username), 
                                      new SqlParameter("@password", password),
                                      new SqlParameter("@name", name),
                                      new SqlParameter("@sex", sex),
                                      new SqlParameter("@age", age),
                                      new SqlParameter("@level", level),
                                      new SqlParameter("@reference", reference),
                                      new SqlParameter("@adr", adr),
                                      new SqlParameter("@tel", tel)                                    
                                    };
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
        Response.Write(@"<script>alert('注册成功！');</script>"); 
    }
    protected bool checkAccountExit()
    {
        bool ans  = true;

        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrCustomer"].ConnectionString;


        string username = AccountTex.Text;

        string sqlString = "select * from Customer_Account where Aaccount = @username";
        SqlParameter[] parameters = {
                                        new SqlParameter("@username",username)
                                    };
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    DataSet ds = new DataSet();
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    adapter.Fill(ds);
                    DataTable table = ds.Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        return true;
                    }
                    else
                        return false;
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }

        }
        return ans;
    }
}